﻿using System;

namespace Geometria.Angulos
{
    public struct Radian : IEquatable<Radian>
    {
        #region Fields

        public const double MaxValue = 3.14159;
        public const double MinValue = -3.14159;

        private double _angulo;

        #endregion Fields

        #region Constructors

        public Radian(double value)
        {
            this._angulo = value;
        }

        public Radian(Sexagesimal value)
        {
            this._angulo = Radian.MaxValue / Sexagesimal.MaxValue * (double)value;
        }

        public Radian(Centesimal value)
        {
            this._angulo = Radian.MaxValue / Centesimal.MaxValue * (double)value;
        }

        #endregion Constructors

        #region Methods

        public static implicit operator double(Radian instance)
        {
            return instance._angulo;
        }

        public static implicit operator Radian(double value)
        {
            return new Radian(value);
        }

        public static Radian Normalize(double value)
        {
            double temp = value;

            while (temp > MaxValue)
                temp -= MaxValue - MinValue;

            while (temp < MinValue)
                temp += MaxValue - MinValue;

            return new Radian(temp);
        }

        public static Radian ParseXY(double x, double y)
        {
            return new Radian(Math.Atan2(x, y));
        }

        public bool Equals(Radian other)
        {
            return Normalize(this)._angulo == Normalize(other._angulo);
        }

        #endregion Methods
    }
}